<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
    <title>WebSocket测试</title>
    <script>
        var ws = null;
        function send() {
            var message = document.getElementById("text").value;
            ws.send(message);
        }
        function connect() {
            if(ws == null)
            {
                ws = new WebSocket("ws://127.0.0.1:8080/ws/123");
                ws.onopen = function ()
                {
                    alert("连接成功");
                };
                ws.onmessage = function (evt)
                {
                    var received_msg = evt.data;
                    document.getElementById("showMes").value += evt.data + "\n";
                };
                ws.onclose = function ()
                {
                    alert("断开了连接");
                };
            }
        }
        function closeSocket()
        {
            ws.close();
            ws = null;
        }
    </script>
</head>

<body>
<input type="button" onclick="connect()" value="连接" />
<input type="button" onclick="closeSocket()" value="关闭" />
<input type="button" onclick="send()" value="发送" />
<input type="text" id="text" />
<br/>
<textarea rows="3" cols="30" id="showMes" style="width:100%;height:500px;"></textarea>
</body>
</html>